package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class XiuGaiMMActivity extends Activity {

    private TextView tv;
    private EditText et;
    private EditText et1;
    private EditText et2;
    private String s;
    private String h;
    private MyApplication m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiu_gai_mm);
        info();
    }
    private void info() {
        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_mykaiguan);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        m = (MyApplication) getApplication();
        tv = (TextView) findViewById(R.id.tv_xiugai);
        et = (EditText) findViewById(R.id.et_xiugai);
        et1 = (EditText) findViewById(R.id.et1_xiugai);
        et2 = (EditText) findViewById(R.id.et2_xiugai);
        SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
        s = sp.getString("username", null);
        h = sp.getString("password", null);
        tv.setText(s);
    }
    public void click(View v) {
        String k = et.getText().toString();
        String l = et1.getText().toString();
        String w = et2.getText().toString();
        if ("".equals(et.getText().toString())) {
            HttpConnection.showToast(getBaseContext(),"请输入原密码");
        } else if (k.equals(l)) {
            HttpConnection.showToast(getBaseContext(),"新密码与老密码相同");
        } else if (!k.equals(h)) {
            HttpConnection.showToast(getBaseContext(),"原密码错误");
        } else if (!stringFilter(l)) {
            HttpConnection.showToast(getBaseContext(),"密码只能输入8—15位数字字母下划线");
        } else if (!l.equals(w)) {
            HttpConnection.showToast(getBaseContext(),"密码不一致，请重新输入");
        } else {
            UserClass u = new UserClass(s, l);
            String j = JSON.toJSONString(u);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/resetPassword", params,
                    new RequestCallBack<String>() {
                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            HttpConnection.showToast(getBaseContext(),"网络异常");
                        }
                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if(arg0.result != null && !"".equals(arg0.result)){
                            arg0.result = AESUtil.decrypt(arg0.result);
                            JSONObject j = JSONObject.parseObject(arg0.result);
                            if ("0".equals(j.getString("status"))) {
                                SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
                                sp.edit().putString("Mac", null).commit();
                                sp.edit().putString("UserId", null).commit();
                                sp.edit().putString("SocketToken", null).commit();
                                sp.edit().putBoolean("isexit", true).commit();
                                sp.edit().putString("password",null).commit();
                                m.userId = null;
                                Intent logoutIntent = new Intent(XiuGaiMMActivity.this, LoginActivity.class);
                                logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(logoutIntent);
                                finish();
                            }else{
                                HttpConnection.showToast(XiuGaiMMActivity.this,"服务器错误");
                            }

                        }else{
                                HttpConnection.showToast(XiuGaiMMActivity.this,"服务器错误");
                            }
                        }
                    });

        }

    }

    public static boolean stringFilter(String str) throws PatternSyntaxException {
        // 只允许字母和数字
        String regEx = "^[a-zA-Z0-9]{8,15}+$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.matches();
    }

}
